package com.example.alarmdemo;


import android.app.Activity;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;


import com.topeet.serialtest.serial;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


public class AlarmUtils {
    private int[] closeLight = {0xfb, 0x00, 0xc1, 0x00, 0x00, 0x00, 0x00, 0xfd};//关
    private int[] openLight = {0xfb, 0x00, 0xc1, 0x00, 0x00, 0x00, 0x01, 0xfd};// 开灯
    private boolean status = true;//记录闪光灯状态
    serial mSerial;
    TextToSpeechUtil mTextToSpeechUtil;
    MediaPlayer player;
    ArrayList<AlarmInfo> list;
    Context context;
    Handler handler;

    public AlarmUtils() {
        mSerial = new serial();
        mSerial.Open(4, 115200);//串口4，波特率115200
    }

    public void flashLight() {
        mSerial.Write(openLight, openLight.length);

    }
//    public Runnable flashLight(final Handler handler, Context context){
//
//        final FlashUtils flashUtils=new FlashUtils(context);
//
//        Runnable r = new Runnable() {
//
//            @Override
//            public void run() {
//
//                flashUtils.converse();
//                //每隔500ms循环执行run方法
//                handler.postDelayed(this, 200);
//
//            }
//        };
//
//        return  r;
//    }

    public void closeLight() {
        mSerial.Write(closeLight, openLight.length);
    }

    public Runnable flash() {

        ContentResolver contentResolver = context.getContentResolver();
        boolean automicBrightness = false;
        try {
            automicBrightness = Settings.System.getInt(contentResolver,
                    Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        int value = 0;
        if (automicBrightness) {
            value = Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;//关闭
        }
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                value);


        Runnable r = new Runnable() {

            @Override
            public void run() {

                converse(contentResolver);
                //每隔500ms循环执行run方法
                handler.postDelayed(this, 200);

            }
        };
        handler.postDelayed(r, 200);
        return r;


    }

    private void converse(ContentResolver contentResolver) {

        Uri uri = Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);

        if (status) {
            Settings.System.putInt(contentResolver,
                    Settings.System.SCREEN_BRIGHTNESS, 255);
            status = false;
        } else {
            Settings.System.putInt(contentResolver,
                    Settings.System.SCREEN_BRIGHTNESS, 0);
            status = true;
        }
        contentResolver.notifyChange(uri, null);
    }


    public void closeLight(Handler mHandler, Runnable runnable, Context context) {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
        mHandler.removeCallbacks(runnable);
    }

    public void speech() {
        mTextToSpeechUtil = TextToSpeechUtil.getInstance(context);

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < list.size(); i++) {
                    mTextToSpeechUtil.speak(list.get(i).getMessage());
                }
            }
        }, 1000);

    }

    public void playSongs() {

        player = MediaPlayer.create(context, R.raw.alarm);
        player.start();
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer arg0) {
                player.start();
                player.setLooping(true);
            }
        });
    }

    public void closeSongs() {
        player.stop();
    }


    public void set(Context context, Handler mHandler, ArrayList<AlarmInfo> list) {
        this.context = context;
        this.handler = mHandler;
        this.list = list;
    }

    public void closeSpeech() {
        mTextToSpeechUtil.stop();

    }
}
